package com.ly.lypopupwindowdemo.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.ly.lypopupwindowdemo.R;

/**
 * Created by liying on 2017/7/14.
 */

public class LyLineView extends View {
    private Paint mPaint;

    public LyLineView(Context context) {
        super(context);
    }

    public LyLineView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        mPaint = new Paint();
    }

    public LyLineView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        /////http://www.cnblogs.com/xiaoxiaing/p/5842762.html

        /**
         * 绘制圆形：drawCircle(float cx, float cy, float radius, Paint paint)
         cx： 圆心的x坐标
         cy： 圆心的y坐标
         radius： 圆的半径
         paint： 绘制风格
         */
        mPaint.setAntiAlias(true);
        mPaint.setColor(getResources().getColor(android.R.color.holo_blue_light));

        canvas.drawCircle(200, 200, 150, mPaint);

        mPaint.setColor(getResources().getColor(android.R.color.holo_orange_light));
        canvas.drawCircle(200, 200, 100, mPaint);

        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(20);

        canvas.drawCircle(400, 400, 150, mPaint);

        /**
         * 绘制矩形：drawRect(float left, float top, float right, float bottom, Paint paint)
         * / drawRect(RectF rect, Paint paint)
         *
         left： 矩形left的x坐标
         top： 矩形top的y坐标
         right： 矩形right的x坐标
         bottom： 矩形bottom的y坐标
         paint： 绘制风格
         */

        mPaint.setColor(getResources().getColor(android.R.color.holo_green_light));
        canvas.drawRect(200, 200, 400, 400, mPaint);


        mPaint.setColor(getResources().getColor(android.R.color.holo_red_light));
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(20);

        RectF rectF = new RectF(20, 30, 650, 650);
        canvas.drawRect(rectF, mPaint);


        /**
         * 绘制圆角矩形：drawRoundRect(float left, float top, float right, float bottom, float rx, float ry, Paint paint) （不可用）/
         *
         * drawRoundRect(RectF rect, float rx, float ry, Paint paint)（可用）

         left： 图形left的x坐标
         top： 图形top的y坐标
         right： 图形right的x坐标
         bottom： 图形bottom的y坐标
         rx： x方向的圆角半径
         ry： y方向的圆角半径
         paint > 绘制风格
         */
        //注释：
        //http://blog.csdn.net/bailin0309/article/details/70232355
        /**
         * Java.lang.NoSuchMethodError: Android.graphics.Canvas.drawRoundRect
         java.lang.NoSuchMethodError: android.widget.PopupWindow.showAsDropDown

         今天开发遇到两个版本问题：
         1 画圆角canvas.drawRoundRect(0,0,vTopWidth,vTopHeight,radius,radius,p);
         4.3以下会出现NoSuchMethodError异常
         需要换canvas.drawRoundRect(new RectF(0,0,vTopWidth,vTopHeight),radius,radius,p)这种写法

         2 在android4.3以下
         popupWindow.showAsDropDown(mViewAction, 0,0,Gravity.CENTER) 出现NoSuchMethodError
         不能加最后一个参数，换成mPopupWindow.showAsDropDown(mViewAction, 0,0);
         */

        mPaint.setColor(getResources().getColor(android.R.color.holo_red_light));
        mPaint.setStrokeWidth(5);
        RectF rectF1 = new RectF(20, 700, 600, 1000);
        canvas.drawRoundRect(rectF1, 30, 30, mPaint);

        /**
         * 绘制弧
         *
         * 绘制弧：drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint paint)

         oval： 指定圆弧的外轮廓矩形区域
         startAngle： 圆弧起始角度，单位为度
         sweepAngle： 圆弧扫过的角度，顺时针方向，单位为度
         useCenter： 如果为True时，在绘制圆弧时将圆心包括在内，通常用来绘制扇形
         paint： 绘制风格
         */

        /**
         * 需要注意的是：最顶弧度270度，地图东方向是0度。
         */

        Paint paint = new Paint();
        //去锯齿
        paint.setAntiAlias(true);
        //设置颜色
        paint.setColor(getResources().getColor(R.color.colorAccent));
        RectF rel = new RectF(700, 100, 900, 300);
        // 设置为圆角

        paint.setStrokeCap(Paint.Cap.ROUND);
        //实心圆弧
        canvas.drawArc(rel, 0, 270, false, paint);
        //实心圆弧 将圆心包含在内
        RectF rel2 = new RectF(700, 400, 900, 600);
        canvas.drawArc(rel2, 0, 270, true, paint);
        //设置空心Style
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(20);
        RectF rel3 = new RectF(700, 700, 900, 900);
        canvas.drawArc(rel3, 0, 270, false, paint);
        RectF rel4 = new RectF(700, 1000, 900, 1200);
        canvas.drawArc(rel4, 0, 270, true, paint);
        
        /**
         * 画文字
         */
        Paint textPaint = new Paint();
        textPaint.setAntiAlias(true);
        textPaint.setColor(Color.BLUE);
        textPaint.setTextSize(50);
        String text = "我的前半生";
        canvas.drawText(text, 192, 862, textPaint);
    }
}
